//
//  ViewController.swift
//  C15072901_FileSandbox
//
//  Created by yao_yu on 15/7/29.
//  Copyright © 2015年 yao_yu. All rights reserved.
//

import UIKit

class ViewController: UIViewController {
    
    var resultLabel = UILabel()

    override func viewDidLoad() {
        super.viewDidLoad()

        view.addSubview(resultLabel)
        resultLabel.numberOfLines =  100
        showInfos()
    }
    
    func showInfos() {
        var result = ""
        result += MyFileManager.documentsPath
        result += MyFileManager.documentationDirectory
        result += "NSHomeDirectory:\(NSHomeDirectory()) \r\n"
        result += "NSTemporaryDirectory:\(NSTemporaryDirectory()) \r\n"
        print(result)
        resultLabel.text = result
        print(NSFileManager.defaultManager().fileExistsAtPath(""))
    }
    
    override func viewDidLayoutSubviews() {
        resultLabel.frame = view.frame
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

class MyFileManager{
    static var documentsPath:String {
        let paths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)
        return "Document:\(paths[0]) \r\n"
    }
    
    static var documentationDirectory:String {
        let paths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentationDirectory, NSSearchPathDomainMask.UserDomainMask, true)
        return "Documentation:\(paths[0]) \r\n"
    }
}

/*
//获得document

+(NSString *)documentsPath {

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

return [paths objectAtIndex:0];

}



//读取工程文件

+(NSString *) ProductPath:(NSString*)filename{



NSString *path = [[NSBundlemainBundle] pathForResource:filename ofType:@""];

return  path;

}



//获得document文件路径，名字方便记忆

+(NSString *) DocumentPath:(NSString *)filename {

NSString *documentsPath = [self documentsPath];

// NSLog(@"documentsPath=%@",documentsPath);

return [documentsPath stringByAppendingPathComponent:filename];

}





//获得document文件路径

+(NSString *)fullpathOfFilename:(NSString *)filename {

NSString *documentsPath = [self documentsPath];

// NSLog(@"documentsPath=%@",documentsPath);

return [documentsPath stringByAppendingPathComponent:filename];

}





//写入文件沙盒位置NSDictionary

+(void)saveNSDictionaryForDocument:(NSDictionary *)list  FileUrl:(NSString*) FileUrl  {



NSString *f = [self fullpathOfFilename:FileUrl];



[list writeToFile:f atomically:YES];

}



//写入文件存放到工程位置NSDictionary

+(void)saveNSDictionaryForProduct:(NSDictionary *)list  FileUrl:(NSString*) FileUrl  {



NSString *ProductPath =[[NSBundlemainBundle]  resourcePath];

NSString *f=[ProductPath stringByAppendingPathComponent:FileUrl];



[list writeToFile:f atomically:YES];

}



//写入文件 Array 工程

+(void)saveOrderArrayListProduct:(NSMutableArray *)list  FileUrl :(NSString*) FileUrl {



NSString *ProductPath =[[NSBundlemainBundle]  resourcePath];

NSString *f=[ProductPath stringByAppendingPathComponent:FileUrl];



[list writeToFile:f atomically:YES];

}

//写入文件 Array 沙盒

+(void)saveOrderArrayList:(NSMutableArray *)list  FileUrl :(NSString*) FileUrl {

NSString *f = [self fullpathOfFilename:FileUrl];



[list writeToFile:f atomically:YES];

}





//加载文件沙盒NSDictionary

+(NSDictionary *)loadNSDictionaryForDocument  : (NSString*) FileUrl {



NSString *f = [self fullpathOfFilename:FileUrl];

NSDictionary *list = [ [NSDictionaryalloc] initWithContentsOfFile:f];



return [list autorelease];

}



//加载文件工程位置NSDictionary

+(NSDictionary *)loadNSDictionaryForProduct   : (NSString*) FileUrl {



NSString *f = [self ProductPath:FileUrl];

NSDictionary *list =[NSDictionarydictionaryWithContentsOfFile:f];



return list;

}





//加载文件沙盒NSArray

+(NSArray *)loadArrayList   : (NSString*) FileUrl {



NSString *f = [self fullpathOfFilename:FileUrl];



NSArray *list = [NSArray  arrayWithContentsOfFile:f];



return list;

}



//加载文件工程位置NSArray

+(NSArray *)loadArrayListProduct   : (NSString*) FileUrl {



NSString *f = [self ProductPath:FileUrl];



NSArray *list = [NSArray  arrayWithContentsOfFile:f];



return list;

}



//拷贝文件到沙盒

+(int) CopyFileToDocument:(NSString*)FileName{





NSString *appFileName =[self fullpathOfFilename:FileName];





NSFileManager *fm = [NSFileManagerdefaultManager];



//判断沙盒下是否存在

BOOL isExist = [fm fileExistsAtPath:appFileName];



if (!isExist)   //不存在，把工程的文件复制document目录下

{



//获取工程中文件

NSString *backupDbPath = [[NSBundle mainBundle]

pathForResource:FileName

ofType:@""];





//这一步实现数据库的添加，

// 通过NSFileManager 对象的复制属性，把工程中数据库的路径复制到应用程序的路径上

BOOL cp = [fm copyItemAtPath:backupDbPath toPath:appFileName error:nil];





return cp;



} else {



return  -1; //已经存在

}



}



//判断文件是否存在

+(BOOL) FileIsExists:(NSString*) checkFile{



if([[NSFileManagerdefaultManager]fileExistsAtPath:checkFile])

{

return true;

}

return  false;



}*/